{
 "cells": [
  {
   "cell_type": "raw",
   "id": "hired-eleven",
   "metadata": {},
   "source": [
    "多重索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "democratic-dependence",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([('000001', 2016),\n",
       "            ('000001', 2017),\n",
       "            ('600000', 2016),\n",
       "            ('600000', 2017),\n",
       "            ('688001', 2016),\n",
       "            ('688001', 2017)],\n",
       "           names=['code', 'year'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "numbers = ['000001', '600000', '688001']\n",
    "colors = [2016, 2017]\n",
    "mindex = pd.MultiIndex.from_product([numbers, colors],  names=['code', 'year'])\n",
    "mindex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "missing-dutch",
   "metadata": {},
   "source": [
    "数据周期变化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "stunning-agreement",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://waditu.com/document/2\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>turnoverratio</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-06-22 13:45</td>\n",
       "      <td>5111.40</td>\n",
       "      <td>5106.74</td>\n",
       "      <td>5111.87</td>\n",
       "      <td>5106.74</td>\n",
       "      <td>2487094.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-06-22 13:50</td>\n",
       "      <td>5107.13</td>\n",
       "      <td>5105.92</td>\n",
       "      <td>5107.90</td>\n",
       "      <td>5105.32</td>\n",
       "      <td>2060726.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-06-22 13:55</td>\n",
       "      <td>5105.85</td>\n",
       "      <td>5091.61</td>\n",
       "      <td>5105.85</td>\n",
       "      <td>5091.61</td>\n",
       "      <td>2860797.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-06-22 14:00</td>\n",
       "      <td>5091.55</td>\n",
       "      <td>5093.93</td>\n",
       "      <td>5095.35</td>\n",
       "      <td>5089.99</td>\n",
       "      <td>2209011.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-06-22 14:05</td>\n",
       "      <td>5094.00</td>\n",
       "      <td>5095.96</td>\n",
       "      <td>5096.88</td>\n",
       "      <td>5093.24</td>\n",
       "      <td>1931971.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               date     open    close     high      low     volume amount  \\\n",
       "0  2021-06-22 13:45  5111.40  5106.74  5111.87  5106.74  2487094.0     {}   \n",
       "1  2021-06-22 13:50  5107.13  5105.92  5107.90  5105.32  2060726.0     {}   \n",
       "2  2021-06-22 13:55  5105.85  5091.61  5105.85  5091.61  2860797.0     {}   \n",
       "3  2021-06-22 14:00  5091.55  5093.93  5095.35  5089.99  2209011.0     {}   \n",
       "4  2021-06-22 14:05  5094.00  5095.96  5096.88  5093.24  1931971.0     {}   \n",
       "\n",
       "  turnoverratio    code  \n",
       "0        0.0000  399300  \n",
       "1        0.0000  399300  \n",
       "2        0.0000  399300  \n",
       "3        0.0000  399300  \n",
       "4        0.0000  399300  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tushare as ts\n",
    "\n",
    "df = ts.get_k_data('399300', ktype='5')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "young-marijuana",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>turnoverratio</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-06-22 13:45:00</th>\n",
       "      <td>5111.40</td>\n",
       "      <td>5106.74</td>\n",
       "      <td>5111.87</td>\n",
       "      <td>5106.74</td>\n",
       "      <td>2487094.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 13:50:00</th>\n",
       "      <td>5107.13</td>\n",
       "      <td>5105.92</td>\n",
       "      <td>5107.90</td>\n",
       "      <td>5105.32</td>\n",
       "      <td>2060726.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 13:55:00</th>\n",
       "      <td>5105.85</td>\n",
       "      <td>5091.61</td>\n",
       "      <td>5105.85</td>\n",
       "      <td>5091.61</td>\n",
       "      <td>2860797.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 14:00:00</th>\n",
       "      <td>5091.55</td>\n",
       "      <td>5093.93</td>\n",
       "      <td>5095.35</td>\n",
       "      <td>5089.99</td>\n",
       "      <td>2209011.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 14:05:00</th>\n",
       "      <td>5094.00</td>\n",
       "      <td>5095.96</td>\n",
       "      <td>5096.88</td>\n",
       "      <td>5093.24</td>\n",
       "      <td>1931971.0</td>\n",
       "      <td>{}</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>399300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        open    close     high      low     volume amount  \\\n",
       "date                                                                        \n",
       "2021-06-22 13:45:00  5111.40  5106.74  5111.87  5106.74  2487094.0     {}   \n",
       "2021-06-22 13:50:00  5107.13  5105.92  5107.90  5105.32  2060726.0     {}   \n",
       "2021-06-22 13:55:00  5105.85  5091.61  5105.85  5091.61  2860797.0     {}   \n",
       "2021-06-22 14:00:00  5091.55  5093.93  5095.35  5089.99  2209011.0     {}   \n",
       "2021-06-22 14:05:00  5094.00  5095.96  5096.88  5093.24  1931971.0     {}   \n",
       "\n",
       "                    turnoverratio    code  \n",
       "date                                       \n",
       "2021-06-22 13:45:00        0.0000  399300  \n",
       "2021-06-22 13:50:00        0.0000  399300  \n",
       "2021-06-22 13:55:00        0.0000  399300  \n",
       "2021-06-22 14:00:00        0.0000  399300  \n",
       "2021-06-22 14:05:00        0.0000  399300  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['date'] = pd.to_datetime(df['date'])\n",
    "\n",
    "df = df.set_index('date')\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "underlying-priest",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-06-22 13:45:00</th>\n",
       "      <td>5111.40</td>\n",
       "      <td>5111.87</td>\n",
       "      <td>5091.61</td>\n",
       "      <td>5091.61</td>\n",
       "      <td>7408617.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 14:00:00</th>\n",
       "      <td>5091.55</td>\n",
       "      <td>5099.31</td>\n",
       "      <td>5089.99</td>\n",
       "      <td>5099.31</td>\n",
       "      <td>5981547.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 14:15:00</th>\n",
       "      <td>5099.04</td>\n",
       "      <td>5110.41</td>\n",
       "      <td>5094.83</td>\n",
       "      <td>5110.41</td>\n",
       "      <td>5570625.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 14:30:00</th>\n",
       "      <td>5111.00</td>\n",
       "      <td>5122.57</td>\n",
       "      <td>5109.61</td>\n",
       "      <td>5118.59</td>\n",
       "      <td>5777052.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-22 14:45:00</th>\n",
       "      <td>5119.01</td>\n",
       "      <td>5121.63</td>\n",
       "      <td>5116.54</td>\n",
       "      <td>5121.12</td>\n",
       "      <td>7400475.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        open     high      low    close     volume\n",
       "date                                                              \n",
       "2021-06-22 13:45:00  5111.40  5111.87  5091.61  5091.61  7408617.0\n",
       "2021-06-22 14:00:00  5091.55  5099.31  5089.99  5099.31  5981547.0\n",
       "2021-06-22 14:15:00  5099.04  5110.41  5094.83  5110.41  5570625.0\n",
       "2021-06-22 14:30:00  5111.00  5122.57  5109.61  5118.59  5777052.0\n",
       "2021-06-22 14:45:00  5119.01  5121.63  5116.54  5121.12  7400475.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_new = pd.DataFrame()\n",
    "\n",
    "#取最前面的作为Open\n",
    "df_new['open'] = df['open'].resample('15T').first()\n",
    "\n",
    "#取最大的作为high\n",
    "df_new['high'] = df['high'].resample('15T').max()\n",
    "\n",
    "#最小的low\n",
    "df_new['low'] = df['low'].resample('15T').min()\n",
    "\n",
    "#最后close\n",
    "df_new['close'] = df['close'].resample('15T').last()\n",
    "\n",
    "#加总\n",
    "df_new['volume'] = df['volume'].resample('15T').sum()\n",
    "\n",
    "df_new.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "higher-robertson",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
